package com.itheima.controller;


import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/names")
    public Result getManagerNames() {
        List<Map<String, Object>> list = memberService.findManagerByRoleId();
        return Result.success(list, "查询健康管理师成功");
    }


    @PostMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        //创建工作簿 类似于创建Excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建 sheetname页名
        XSSFSheet sheet = workbook.createSheet("会员信息");
        sheet.setColumnWidth(3, 20 * 256);//给第3列设置为20个字的宽度
        sheet.setColumnWidth(4, 20 * 256);//给第4列设置为20个字的宽度
        //创建一行,下标从0开始
        XSSFRow row = sheet.createRow(0);
        //创建这行中的列,下标从0开始 （表头）
        XSSFCell cell = row.createCell(0);
        // 给cell 0下表赋值
        cell.setCellValue("档案号");
        //创建这行中的列,并给该列直接赋值
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("性别");
        row.createCell(3).setCellValue("年龄");
        row.createCell(4).setCellValue("健康管理师");
        row.createCell(5).setCellValue("注册时间");
        row.createCell(6).setCellValue("手机号码");
        // 设置表里内容
        List<Member> members = memberService.findAll();
        for (int i = 1; i <= members.size(); i++) {
            Date date = new Date(System.currentTimeMillis());
            row = sheet.createRow(i+1);
            int age = date.getYear() - members.get(i).getBirthday().getYear();
            row.createCell(0).setCellValue(members.get(i).getFileNumber());
            row.createCell(1).setCellValue(members.get(i).getName());
            row.createCell(2).setCellValue(members.get(i).getSex());
            row.createCell(3).setCellValue(age);
            row.createCell(4).setCellValue(members.get(i).getManager());
            row.createCell(5).setCellValue(members.get(i).getRegTime());
            row.createCell(6).setCellValue(members.get(i).getPhoneNumber());
        }
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
